package com.dhcc.bpm.modules.scf.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.segmentId.LocalCacheIdGenerator;
import com.dhcc.bpm.modules.scf.dto.ScfRecyclingWeighInfoDTO;
import com.dhcc.bpm.modules.scf.dto.ScfRenewableOutInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfOperationLogInfo;
import com.dhcc.bpm.modules.scf.entity.ScfRecyclingWeighInfo;
import com.dhcc.bpm.modules.scf.entity.SelectEntity.RecyclingWeighByRenewableOut;
import com.dhcc.bpm.modules.scf.enumUtil.ScfOperationLogInfoEnum;
import com.dhcc.bpm.modules.scf.mapper.ScfDriverInfoMapper;
import com.dhcc.bpm.modules.scf.mapper.ScfRecyclingWeighInfoMapper;
import com.dhcc.bpm.modules.scf.service.IScfFileAttachmentInfoService;
import com.dhcc.bpm.modules.scf.service.IScfOperationLogInfoService;
import com.dhcc.bpm.modules.scf.service.IScfRecyclingWeighInfoService;
import com.dhcc.bpm.modules.scf.service.IScfRenewableOutInfoService;
import com.dhcc.bpm.modules.scf.util.DateConversionExample;
import com.dhcc.bpm.modules.scf.util.ExcelUtil;
import com.dhcc.bpm.modules.scf.util.MD5Utils;
import com.dhcc.bpm.modules.scf.util.OBSUtil;
import com.dhcc.bpm.modules.scf.vo.ScfDriverInfoVO.ScfGetDriverDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfFileAttachmentInfoVO.ScfFileAttachmentInfoVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingWeighInfoVO.ScfGetRecyclingWeighDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingWeighInfoVO.ScfGetRecyclingWeighListVO;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableOutInfoVO.ScfGetRenewableOutDetailsVO;
import com.dhcc.bpm.modules.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 利废企业过磅服务层实现类
 */
@Service
@Slf4j
public class ScfRecyclingWeighInfoServiceImpl extends ServiceImpl<ScfRecyclingWeighInfoMapper, ScfRecyclingWeighInfo> implements IScfRecyclingWeighInfoService {
    /**
     * 文件附件service接口
     */
    @Autowired
    private IScfFileAttachmentInfoService scfFileAttachmentInfoService;
    /**
     * id生成器
     */
    @Autowired
    private LocalCacheIdGenerator localCacheIdGenerator;
    /**
     * 再生出库信息service接口
     */
    @Autowired
    private IScfRenewableOutInfoService scfRenewableOutInfoService;
    /**
     * 操作日志service接口
     */
    @Autowired
    private IScfOperationLogInfoService scfOperationLogInfoService;
    /**
     * 司机信息service接口
     */
    @Autowired
    private ScfDriverInfoMapper scfDriverInfoMapper;
    /**
     * 用户信息service接口
     */
    @Autowired
    private IUserService userService;
    /**
     * obs对象存储服务工具类
     */
    @Autowired
    private OBSUtil obsUtil;

    /**
     * 利废企业过磅单分页查询列表
     *
     * @param page
     * @param scfRecyclingWeighInfo
     * @return
     */
    @Override
    public IPage<ScfGetRecyclingWeighListVO> selectScfRecyclingWeighInfoPage(IPage<ScfGetRecyclingWeighListVO> page, ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) {
        List<ScfGetRecyclingWeighListVO> scfGetRecyclingWeighListVOS;
        String isRelated = scfRecyclingWeighInfo.getIsRelated();
        if (isRelated != null && isRelated.equals("3")) {
            // 查询利废过磅分页数据 -- 未关联
            scfGetRecyclingWeighListVOS = baseMapper.selectUnrelatedRecyclingWeighRecords(page, scfRecyclingWeighInfo);
        } else {
            // 查询利废过磅分页数据 -- 全部
            scfGetRecyclingWeighListVOS = baseMapper.selectScfRecyclingWeighInfoPage2(page, scfRecyclingWeighInfo);
        }
        // 判断分页数据数组是否为空
        if (scfGetRecyclingWeighListVOS.size() != 0) {
            // 分页数据不为空则遍历
            for (ScfGetRecyclingWeighListVO scfGetRecyclingWeighListVO : scfGetRecyclingWeighListVOS) {
                // 获取司机信息rowId
                String rowIdByLicense = scfDriverInfoMapper.selectRowIdByLicense(scfGetRecyclingWeighListVO.getLicense());
                if (rowIdByLicense != null) {
                    // 查询司机详情信息
                    ScfGetDriverDetailsVO scfDriverInfoMapperDetails = scfDriverInfoMapper.getDetails(rowIdByLicense);
                    // 拿取司机详情信息中的司机名称并设置到利废过磅单的司机名称
                    scfGetRecyclingWeighListVO.setDriverName(scfDriverInfoMapperDetails.getDriverName());
                }
            }
        }
        return page.setRecords(scfGetRecyclingWeighListVOS);
    }

    /**
     * 利废企业过磅单查询详情
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @Override
    public ScfGetRecyclingWeighDetailsVO getDetail(ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) {
        // 获取利废企业过磅详情信息
        ScfGetRecyclingWeighDetailsVO detail = baseMapper.getDetails(scfRecyclingWeighInfo.getRowId());
        if (detail != null && !detail.getLicense().isEmpty()) {
            // 获取司机记录的rowId
            String rowIdByLicense = scfDriverInfoMapper.selectRowIdByLicense(detail.getLicense());
            // 查询司机详情信息
            ScfGetDriverDetailsVO scfDriverInfoMapperDetails = scfDriverInfoMapper.getDetails(rowIdByLicense);
            if (scfDriverInfoMapperDetails != null) {
                // 拿取司机详情信息中的司机姓名设置到利废过磅单信息对应字段中
                detail.setDriverName(scfDriverInfoMapperDetails.getDriverName());
                // 拿取司机详情信息中的司机电话设置到利废过磅单信息对应字段中
                detail.setDriverMobile(scfDriverInfoMapperDetails.getMobile());
            }


            // 查询并设置利废过磅单与再生出库单对应的关联关系
            List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = baseMapper.selectRecyclingWeighByRenewableOut(detail.getWeighNum());
            // 创建一个再生出库单数组对象
            List<ScfGetRenewableOutDetailsVO> scfGetRenewableOutDetailsVOS = new ArrayList<>();
            // 判断关联关系是否为空
            if (recyclingWeighByRenewableOuts != null) {
                // 不为空则遍历关联关系
                for (RecyclingWeighByRenewableOut recyclingWeighByRenewableOut : recyclingWeighByRenewableOuts) {
                    // 查询再生出库单详情信息
                    ScfRenewableOutInfoDTO scfRenewableOutInfo = new ScfRenewableOutInfoDTO();
                    if("no".equals(recyclingWeighByRenewableOut.getRenewableOutRowId() )){
                        scfRenewableOutInfo.setRowId( recyclingWeighByRenewableOut.getRenewableOutNum());
                    }else {
                        scfRenewableOutInfo.setRowId(recyclingWeighByRenewableOut.getRenewableOutRowId());
                    }


                    ScfGetRenewableOutDetailsVO details = scfRenewableOutInfoService.getDetails(scfRenewableOutInfo);
                    // 将每条再生出库单详情添加到再生出库单数组对象中
                    scfGetRenewableOutDetailsVOS.add(details);
                }
            }
            // 将再生出库单对象数组设置到利废过磅的再生出库单字段中
            detail.setRecyclingWeighByRenewableOuts(scfGetRenewableOutDetailsVOS);
            // 获取与文件附件表关联的id
            String rowId = detail.getRowId();
            // 查询关联的附件信息
            List<ScfFileAttachmentInfoVO> scfFileAttachmentInfoVOS = scfFileAttachmentInfoService.selectAllByAssociationId(rowId);
            // 将附件信息存进dto里
            detail.setFileAttachmentInfoVOS(scfFileAttachmentInfoVOS);
        }
        return detail;
    }

    /**
     * 利废企业过磅单新增
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRecyclingWeighInfo(ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) throws Exception {
        // 判断是否已存在该磅单号码
        String weighNum = scfRecyclingWeighInfo.getWeighNum();
        String rowIdByWeighNum = baseMapper.getRowIdByWeighNum(weighNum);
        if (rowIdByWeighNum != null) {
            // 说明该磅单号码库中存在，抛出异常，提示该榜单号码已存在
            throw new Exception("利废过榜单:" + weighNum + "已存在，请勿重复录入磅单！");
        }
        // 生成日期
        SimpleDateFormat date = new SimpleDateFormat("YYYYMMdd");
        String formatdate = date.format(new Date());
        // 生成时间
        SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
        String formTime = timeFormat.format(new Date());

        // 生成ROW_ID
        String rowId = "RCGB" + localCacheIdGenerator.nextId() + formTime;
        // 开始补充对象字段start
        // 设置记录编号
        scfRecyclingWeighInfo.setRowId(rowId);
        // 设置登记日期
        scfRecyclingWeighInfo.setRecordDate(formatdate);
        // 设置登记时间
        scfRecyclingWeighInfo.setRecordTime(formTime);
        // 补充对象字段结束end
        // 利废过磅单开始关联再生出库单
        List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = scfRecyclingWeighInfo.getRecyclingWeighByRenewableOuts();
        // 判断利废过磅单与再生出库单关联关系是否为空
        if (recyclingWeighByRenewableOuts != null && recyclingWeighByRenewableOuts.size() != 0) {
            // 取出一条关联关系
            RecyclingWeighByRenewableOut recyclingWeighByRenewableOutByOne = recyclingWeighByRenewableOuts.get(0);
            // 获取再生出库单rowId
            String renewableOutRowId = recyclingWeighByRenewableOutByOne.getRowId();
            // new一个再生出库单信息对象
            ScfRenewableOutInfoDTO scfRenewableOutInfoOOne = new ScfRenewableOutInfoDTO();
            // 设置rowId给这个再生出库单对象
            scfRenewableOutInfoOOne.setRowId(renewableOutRowId);
            // 获取再生出库单对象
            ScfGetRenewableOutDetailsVO scfRenewableOutInfoServiceDetails = scfRenewableOutInfoService.getDetails(scfRenewableOutInfoOOne);
            // 获取这条数据的车牌号及驾驶证号
            String license = scfRenewableOutInfoServiceDetails.getLicense();
            String truckNum = scfRenewableOutInfoServiceDetails.getTruckNum();
            // 不为空则遍历关联关系
            for (RecyclingWeighByRenewableOut recyclingWeighByRenewableOut : recyclingWeighByRenewableOuts) {
                // 设置关联关系对象的过磅单号
                recyclingWeighByRenewableOut.setWeighNum(scfRecyclingWeighInfo.getWeighNum());
                // 设置关联关系对象的过磅记录rowId
                recyclingWeighByRenewableOut.setWeighRowId(scfRecyclingWeighInfo.getRowId());
                // 设置再生出库的记录RowId
                recyclingWeighByRenewableOut.setRenewableOutRowId(recyclingWeighByRenewableOut.getRowId());
                // 设置该关联关系记录的主键rowId
//                recyclingWeighByRenewableOut.setRowId("GL" + localCacheIdGenerator.nextId() + formTime);
                recyclingWeighByRenewableOut.setRowId("GBD_ZSCK" + MD5Utils.md5(recyclingWeighByRenewableOut.getWeighNum() + recyclingWeighByRenewableOut.getRenewableOutNum()));

                // 设置字段补充完成
                // 校验关联关系数组中的所有再生出库单，司机与车辆是否都相同
                // new一个再生出库单信息对象
                ScfRenewableOutInfoDTO scfRenewableOutInfo = new ScfRenewableOutInfoDTO();
                // 设置rowId给这个再生出库单对象
                scfRenewableOutInfo.setRowId(recyclingWeighByRenewableOut.getRenewableOutRowId());
                // 获取关联的再生出库单信息
                ScfGetRenewableOutDetailsVO details = scfRenewableOutInfoService.getDetails(scfRenewableOutInfo);
                if (!details.getTruckNum().equals(truckNum)) {
                    throw new Exception("选择的再生资源出库单车辆不相同，请重新选择！");
                }
                if (!details.getLicense().equals(license)) {
                    throw new Exception("选择的再生资源出库单司机不相同，请重新选择！");
                }
                // 存储关联关系
                baseMapper.saveRecyclingWeighByRenewableOut(recyclingWeighByRenewableOut);
            }
            // 校验通过 设置利废过磅单的车牌号
            scfRecyclingWeighInfo.setTruckNum(truckNum);
            // 设置利废过磅单的司机驾驶证号
            scfRecyclingWeighInfo.setLicense(license);
        }
        // 存储利废企业过磅单信息
        boolean isSave = save(scfRecyclingWeighInfo);
        // 获取过磅单中的附件文件编号数组
        List<String> rowIds = scfRecyclingWeighInfo.getRowIds();
        // 设置过磅单文件存放路径
        String path = "GB" + "/" + rowId + "/";
        if (rowIds != null) {
            if (rowIds.size() != 0) {
                if (isSave) {
                    for (String fileRowId : rowIds) {
                        // 根据文件id获取文件的临时objectKey
                        String objectKeyOfTemp = scfFileAttachmentInfoService.getFilePath(fileRowId,null);
                        // 截取文件的名称
                        String extractFileName = obsUtil.extractFileName(objectKeyOfTemp);
                        // 拼接正式的objectKey
                        String objectKey = path + extractFileName;
                        // 将附件文件从临时文件夹转移至正式文件夹
                        obsUtil.copyFile(objectKey, objectKeyOfTemp);
                        scfFileAttachmentInfoService.updateFilePath(fileRowId, objectKey);
                    }
                }
            }

            scfFileAttachmentInfoService.batchUpdateField(rowIds, rowId);
        }
        // 生成日期时间
        SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = datetime.format(new Date());
        ScfOperationLogInfo scfOperationLogInfo = new ScfOperationLogInfo();
        // 根据用户id查询用户名称，重新赋值登记人字段
        List<Long> ids = new ArrayList<>();
        ids.add(Long.valueOf(scfRecyclingWeighInfo.getRecorder()));
        scfOperationLogInfo.setRecorder(userService.selectUserNameByIds(ids).get(0));
        scfOperationLogInfo.setOperationModule(ScfOperationLogInfoEnum.LOGISTICS.getOperation());
        scfOperationLogInfo.setOperationFunction("利废企业过磅单");
        scfOperationLogInfo.setOperationType(ScfOperationLogInfoEnum.ADD.getOperation());
        scfOperationLogInfo.setOperationContent("新增一条利废企业过磅单，利废企业过磅单号：" + scfRecyclingWeighInfo.getWeighNum());
        scfOperationLogInfo.setRecordDate(formattedDateTime);
        // 判断利废企业过磅单是否新增成功
        if (isSave) {
            // 成功则记录操作日志
            scfOperationLogInfoService.saveOperationLogInfo(scfOperationLogInfo);
        }
        return isSave;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int bachSaveRecyclingWeighInfo(List<ScfRecyclingWeighInfo> scfRecyclingWeighInfos, String operatorId) {
        List<ScfRecyclingWeighInfo> scfRecyclingWeighInfoList = new ArrayList<>();
        List<ScfOperationLogInfo> scfOperationLogInfoList = new ArrayList<>();
        for (ScfRecyclingWeighInfo scfRecyclingWeighInfo : scfRecyclingWeighInfos) {
            // 设置登记人ID
            scfRecyclingWeighInfo.setRecorder(operatorId);
            // 设置过磅类别，默认为利废过磅：2
            scfRecyclingWeighInfo.setPasswayType("2");
            // 生成日期
            SimpleDateFormat date = new SimpleDateFormat("YYYYMMdd");
            String formatdate = date.format(new Date());
            // 生成时间
            SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
            String formTime = timeFormat.format(new Date());

            // 生成ROW_ID
            String rowId = "RCGB" + localCacheIdGenerator.nextId() + formTime;
            // 开始补充对象字段start
            // 设置记录编号
            scfRecyclingWeighInfo.setRowId(rowId);
            // 设置登记日期
            scfRecyclingWeighInfo.setRecordDate(formatdate);
            // 设置登记时间
            scfRecyclingWeighInfo.setRecordTime(formTime);
            // 重新解析正确格式时间字段，并重新赋值
            DateConversionExample dateConversionExample = new DateConversionExample();
            scfRecyclingWeighInfo.setFullTime(dateConversionExample.reParsingTime(scfRecyclingWeighInfo.getFullTime()));
            scfRecyclingWeighInfo.setEmptyTime(dateConversionExample.reParsingTime(scfRecyclingWeighInfo.getEmptyTime()));
            // 补充对象字段结束end
            // 判断是否已存在该磅单号码
            String weighNum = scfRecyclingWeighInfo.getWeighNum();
            String rowIdByWeighNum = baseMapper.getRowIdByWeighNum(weighNum);
            if (rowIdByWeighNum == null) {
                // 说明该磅单号码库中存在，抛出异常，提示该榜单号码已存在
                if(scfRecyclingWeighInfo.getWeighDate() ==null && scfRecyclingWeighInfo.getTruckNum()!=null ){
                    scfRecyclingWeighInfo.setWeighDate( scfRecyclingWeighInfo.getTruckNum());
                    scfRecyclingWeighInfo.setTruckNum(null);
                }

                ScfRenewableOutInfoDTO scfRenewableOutInfoOOne = new ScfRenewableOutInfoDTO();
                // 设置rowId给这个再生出库单对象
                scfRenewableOutInfoOOne.setRowId(scfRecyclingWeighInfo.getRenewableOutNum());


                ScfGetRenewableOutDetailsVO scfRenewableOutInfoServiceDetails = scfRenewableOutInfoService.getDetails(scfRenewableOutInfoOOne);
                // 获取这条数据的车牌号及驾驶证号
                String license = scfRenewableOutInfoServiceDetails.getLicense();
                String truckNum = scfRenewableOutInfoServiceDetails.getTruckNum();

                scfRecyclingWeighInfo.setLicense(license);
                scfRecyclingWeighInfo.setTruckNum(truckNum);
                scfRecyclingWeighInfoList.add(scfRecyclingWeighInfo);
                // 生成日期时间
                SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedDateTime = datetime.format(new Date());
                ScfOperationLogInfo scfOperationLogInfo = new ScfOperationLogInfo();
                scfOperationLogInfo.setRowId("Log" + formatdate + formTime + localCacheIdGenerator.nextId());
                // 根据用户id查询用户名称，重新赋值登记人字段
                List<Long> ids = new ArrayList<>();
                ids.add(Long.valueOf(scfRecyclingWeighInfo.getRecorder()));
                scfOperationLogInfo.setRecorder(userService.selectUserNameByIds(ids).get(0));
                scfOperationLogInfo.setOperationModule(ScfOperationLogInfoEnum.LOGISTICS.getOperation());
                scfOperationLogInfo.setOperationFunction("利废企业过磅单");
                scfOperationLogInfo.setOperationType(ScfOperationLogInfoEnum.ADD.getOperation());
                scfOperationLogInfo.setOperationContent("新增一条利废企业过磅单，利废企业过磅单号：" + scfRecyclingWeighInfo.getWeighNum());
                scfOperationLogInfo.setRecordDate(formattedDateTime);
                scfOperationLogInfoList.add(scfOperationLogInfo);

                RecyclingWeighByRenewableOut gbd2zsck =new RecyclingWeighByRenewableOut();

                gbd2zsck.setWeighNum(scfRecyclingWeighInfo.getWeighNum());
                gbd2zsck.setRenewableOutNum(  scfRecyclingWeighInfo.getRenewableOutNum());
                gbd2zsck.setWeighRowId(scfRecyclingWeighInfo.getRowId());
                gbd2zsck.setRenewableOutRowId("no");
                gbd2zsck.setRowId("GBD_ZSCK" + MD5Utils.md5(gbd2zsck.getWeighNum() + gbd2zsck.getRenewableOutNum()));

                baseMapper.saveRecyclingWeighByRenewableOut(gbd2zsck);

            }
        }
        boolean saveBatch = saveBatch(scfRecyclingWeighInfoList);
//        if (saveBatch) {
//            scfOperationLogInfoService.bachSaveOperationLogInfo(scfOperationLogInfoList);
//        }
        return scfRecyclingWeighInfoList.size();
    }

    /**
     * 利废企业过磅单更新
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRecyclingWeighInfo(ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) throws Exception {
        // 生成日期
        SimpleDateFormat date = new SimpleDateFormat("YYYYMMdd");
        String formatdate = date.format(new Date());
        // 生成时间
        SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
        String formTime = timeFormat.format(new Date());
        // 补充对象字段结束end
        String rowId = scfRecyclingWeighInfo.getRowId();
        List<String> rowIds = scfRecyclingWeighInfo.getRowIds();
        // 设置过磅单文件存放路径
        String path = "GB" + "/" + rowId + "/";
        if (rowId != null) {
            if (!rowIds.isEmpty()) {
                // 取消旧文件与记录关联
                scfFileAttachmentInfoService.updataFiledNull(rowId);
                if (rowIds.size() != 0) {
                    // 给上传的新文件添加关联编号
                    scfFileAttachmentInfoService.batchUpdateField(rowIds, rowId);
                    for (String fileRowId : rowIds) {
                        // 根据文件id获取文件的临时objectKey
                        String objectKeyOfTemp = scfFileAttachmentInfoService.getFilePath(fileRowId,null);
                        // 截取文件的名称
                        String extractFileName = obsUtil.extractFileName(objectKeyOfTemp);
                        // 拼接正式的objectKey
                        String objectKey = path + extractFileName;
                        // 将临时文件夹中的附件转移到正式文件夹
                        obsUtil.copyFile(objectKey, objectKeyOfTemp);
                        scfFileAttachmentInfoService.updateFilePath(fileRowId, objectKey);
                    }
                }
            }
        }
        // 利废过磅单开始关联再生出库单
        List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = scfRecyclingWeighInfo.getRecyclingWeighByRenewableOuts();
        // 判断关联关系是否为空
        if (recyclingWeighByRenewableOuts != null && recyclingWeighByRenewableOuts.size() != 0) {
            // 如果不为空则进入循环
            // 清理旧的关联关系记录，然后建立新的关联关系
            baseMapper.deleteRecyclingWeighByRenewableOut(scfRecyclingWeighInfo.getWeighNum());
            // 取出一条关联关系
            RecyclingWeighByRenewableOut recyclingWeighByRenewableOutByOne = recyclingWeighByRenewableOuts.get(0);
            // 获取再生出库单rowId
            String renewableOutRowId = recyclingWeighByRenewableOutByOne.getRowId();
            // new一个再生出库单信息对象
            ScfRenewableOutInfoDTO scfRenewableOutInfoOOne = new ScfRenewableOutInfoDTO();
            // 设置rowId给这个再生出库单对象
            scfRenewableOutInfoOOne.setRowId(renewableOutRowId);
            // 获取再生出库单对象
            ScfGetRenewableOutDetailsVO scfRenewableOutInfoServiceDetails = scfRenewableOutInfoService.getDetails(scfRenewableOutInfoOOne);
            // 获取这条数据的车牌号及驾驶证号
            String license = scfRenewableOutInfoServiceDetails.getLicense();
            String truckNum = scfRenewableOutInfoServiceDetails.getTruckNum();
            // 遍历关联关系
            for (RecyclingWeighByRenewableOut recyclingWeighByRenewableOut : recyclingWeighByRenewableOuts) {
                // 设置关联关系对象中的过磅单号
                recyclingWeighByRenewableOut.setWeighNum(scfRecyclingWeighInfo.getWeighNum());
                // 设置关联关系对象中的过磅编号RowId
                recyclingWeighByRenewableOut.setWeighRowId(scfRecyclingWeighInfo.getRowId());
                // 设置再生出库单的rowId
                recyclingWeighByRenewableOut.setRenewableOutRowId(recyclingWeighByRenewableOut.getRowId());
                // 设置关联关系记录的主键 rowId
//                recyclingWeighByRenewableOut.setRowId("GL" + localCacheIdGenerator.nextId() + formTime);


                recyclingWeighByRenewableOut.setRowId("GBD_ZSCK" + MD5Utils.md5(recyclingWeighByRenewableOut.getWeighNum() + recyclingWeighByRenewableOut.getRenewableOutNum()));

                // 校验关联关系数组中的所有再生出库单，司机与车辆是否都相同
                // new一个再生出库单信息对象
                ScfRenewableOutInfoDTO scfRenewableOutInfo = new ScfRenewableOutInfoDTO();
                // 设置rowId给这个再生出库单对象
                scfRenewableOutInfo.setRowId(recyclingWeighByRenewableOut.getRenewableOutRowId());
                // 获取关联的再生出库单信息
                ScfGetRenewableOutDetailsVO details = scfRenewableOutInfoService.getDetails(scfRenewableOutInfo);
                if (!details.getTruckNum().equals(truckNum)) {
                    throw new Exception("选择的再生资源出库单车辆不相同，请重新选择！");
                }
                if (!details.getLicense().equals(license)) {
                    throw new Exception("选择的再生资源出库单司机不相同，请重新选择！");
                }
                // 存储关联关系
                baseMapper.saveRecyclingWeighByRenewableOut(recyclingWeighByRenewableOut);
            }
            scfRecyclingWeighInfo.setLicense(license);
            scfRecyclingWeighInfo.setTruckNum(truckNum);
        }
        boolean update = updateById(scfRecyclingWeighInfo);
        // 记录操作日志
        // 生成日期时间
   /*     SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = datetime.format(new Date());
        ScfOperationLogInfo scfOperationLogInfo = new ScfOperationLogInfo();
        scfOperationLogInfo.setRowId("Log" + formatdate + formTime + localCacheIdGenerator.nextId());
        // 根据用户id查询用户名称，重新赋值登记人字段
        List<Long> ids = new ArrayList<>();
        ids.add(Long.valueOf(scfRecyclingWeighInfo.getRecorder()));
        scfOperationLogInfo.setRecorder(userService.selectUserNameByIds(ids).get(0));
        scfOperationLogInfo.setOperationModule(ScfOperationLogInfoEnum.LOGISTICS.getOperation());
        scfOperationLogInfo.setOperationFunction("利废企业过磅单");
        scfOperationLogInfo.setOperationType(ScfOperationLogInfoEnum.UPDATE.getOperation());
        scfOperationLogInfo.setOperationContent("更新一条利废企业过磅单，利废企业过磅单号：" + scfRecyclingWeighInfo.getWeighNum());
        scfOperationLogInfo.setRecordDate(formattedDateTime);*/
        // 判断更新是否成功
//        if (update) {
//            // 成功则记录操作日志
//            scfOperationLogInfoService.saveOperationLogInfo(scfOperationLogInfo);
//        }
        return update;
    }

    /**
     * 利废企业过磅单批量导入
     *
     * @param operatorId
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public List<ScfRecyclingWeighInfo> batchSaveRecyclingWeighInfo(String operatorId, MultipartFile file) throws IOException {
        // 获取文件的Base64编码
        byte[] fileBytes = file.getBytes();
        // 解析excel文件获取数据集合
        List<ScfRecyclingWeighInfo> scfRecyclingWeighInfos = ExcelUtil.readExcel(fileBytes, ScfRecyclingWeighInfo.class, "利废企业过磅单", 3);
        log.info("数据集合为：{}", scfRecyclingWeighInfos);
        return scfRecyclingWeighInfos;
    }

    @Override
    public Boolean deleteByIds(String[] ids) {
        return this.baseMapper.deleteByIds(ids);
    }
}
